<?php
namespace app\index\controller;
use app\index\model\CrmOrder as ThisModel;
use app\index\validate\CrmOrder as ThisValidate;
use think\Exception;

class Order extends Base
{
    public $table;
    public function __construct()
    {
        parent::__construct();
        $this->table = new ThisModel();
    }

    // 新增修改
    public function add()
    {
        try {
            $post = input();//order_type product_id customer_id name phone id_card address defendant_name defendant_phone defendant_id_card defendant_address total ding_price pics audios remark
            $validate = new ThisValidate();
            if ($validate->check($post)) {
                $post['product_id'] = implode(',', $post['product_id']);
                $post['product_name'] = db('crm_product')->where(['id'=>['in',$post['product_id']]])->column('name');
                $post['product_name'] = implode(',', $post['product_name']);
                $post['daishou_price'] = $post['total'] - $post['ding_price'];
                if (isset($post['id'])) {
                    if ($this->table->save($post,['id'=>$post['id']])) {
                        $this->orderRecordAdd($post['id'], $this->username, '修改订单');
                        apiReturn(200,'修改成功');
                    }else{
                        apiReturn(201,'修改失败');
                    }
                }else{
                    $manageInfo = db('crm_manage')->alias('a')->join('tp_crm_fenzu b','a.zu_id=b.id')->field('a.username,a.name,b.id,b.name as zu_name')->where('a.username',$this->username)->find();
                    if (empty($manageInfo)) {
                        apiReturn(201,'工号不存在');
                    }
                    $post['manage_id'] = $manageInfo['username'];
                    $post['manage_name'] = $manageInfo['name'];
                    $post['zu_id'] = $manageInfo['id'];
                    $post['zu_name'] = $manageInfo['zu_name'];
                    // $manageInfo1 = db('crm_manage')->alias('a')->join('tp_crm_fenzu b','a.zu_id=b.id')->field('a.username,a.name,b.id,b.name as zu_name')->where('a.username',$post['houduan_manage_id'])->find();
                    // if (empty($manageInfo)) {
                    //     apiReturn(201,'工号不存在');
                    // }
                    // $post['houduan_manage_id'] = $manageInfo1['username'];
                    // $post['houduan_manage_name'] = $manageInfo1['name'];
                    // $post['houduan_zu_id'] = $manageInfo1['id'];
                    // $post['houduan_zu_name'] = $manageInfo1['zu_name'];
                    $post['date'] = time();
                    $max_orderid = db('crm_order')->max('order_id');
                    $post['order_id'] = (empty($max_orderid)) ? 1 : ($max_orderid+1);
                    if ($order_id = $this->table->allowField(true)->save($post)) {
                        $this->orderRecordAdd($post['order_id'], $this->username, '新增订单');
                        apiReturn(200,'新增成功');
                    }else{
                        apiReturn(201,'新增失败');
                    }
                }
            }else{
                apiReturn(201,$validate->getError());
            }
        } catch (Exception $e) {
            apiReturn(201,$e->getMessage());
        }
    }

    // 列表
    public function list()
    {
        $post = input();
        $limit = isset($post['rows']) ? $post['rows'] : 10;
        $page = isset($post['page']) ? $post['page'] : 1;
        $post['page_type'] = isset($post['page_type']) ? $post['page_type'] : 1;
        $where = [];
        //订单列表
        if ($post['page_type'] == 1) {
            if ($this->is_xiaoshou == 1) {
                if ($this->manage_type == 1) {
                    $where['manage_id'] = $this->getManageList();
                }elseif ($this->manage_type == 2) {
                    $where['houduan_manage_id'] = $this->getManageList();
                }else{
                    
                }
            }
        //订单审核
        }elseif ($post['page_type'] == 2) {
            $where['order_status'] = 0;
        //物流发货
        }elseif ($post['page_type'] == 3) {
            $where['order_type'] = 1;
            $where['order_status'] = 4;
        //法催
        }elseif ($post['page_type'] == 4) {
            $where['order_type'] = 1;
            $where['order_status'] = 5;
        }else{
            apiReturn(201,'参数错误');
        }
        // 组
        if (isset($post['zu_id'])) {
            if (!empty($post['zu_id'])) {
                $wherezu_id = $this->getFenzuList($post['zu_id']);
                $where['manage_id'] = $this->getManageList($wherezu_id);
            }
        }
        // 工号
        if (isset($post['manage_id'])) {
            if (!empty($post['manage_id'])) {
                $where['manage_id'] = $post['manage_id'];
            }
        }
        // 律师助理
        if (isset($post['houduan_manage_id'])) {
            if (!empty($post['houduan_manage_id'])) {
                $where['houduan_manage_id'] = $post['houduan_manage_id'];
            }
        }
        // 创建时间
        if (isset($post['starttime']) && isset($post['endtime'])) {
            if (!empty($post['starttime']) && !empty($post['endtime'])) {
                $where['date'] = array(array('egt',strtotime($post['starttime'])),array('elt',strtotime($post['endtime'])));
            }
        }
        // 订单id
        if (isset($post['order_id'])) {
            if (!empty($post['order_id'])) {
                $where['order_id'] = $post['order_id'];
            }
        }
        // 客户id
        if (isset($post['customer_id'])) {
            if (!empty($post['customer_id'])) {
                $where['customer_id'] = $post['customer_id'];
            }
        }
        // 客户姓名
        if (isset($post['name'])) {
            if (!empty($post['name'])) {
                $where['name'] = ['like','%'.$post['name'].'%'];
            }
        }
        // 客户电话
        if (isset($post['phone'])) {
            if (!empty($post['phone'])) {
                $where['phone'] = ['like','%'.$post['phone'].'%'];
            }
        }
        // 客户地址
        if (isset($post['address'])) {
            if (!empty($post['address'])) {
                $where['address'] = ['like','%'.$post['address'].'%'];
            }
        }
        //订单类型
        if (isset($post['order_type'])) {
            if (!empty($post['order_type'])) {
                $where['order_type'] = $post['order_type'];
            }
        }
        //订单状态
        if (isset($post['order_status'])) {
            if ($post['order_status'] != '') {
                $where['order_status'] = $post['order_status'];
            }
        }
        //订单金额
        if (isset($post['total'])) {
            if ($post['total'] != '') {
                $where['total'] = $post['total'];
            }
        }

        $list = $this->table->where($where)->order('id DESC')->page($page)->limit($limit)->select();
        $count = $this->table->where($where)->count();
        foreach ($list as $key => &$value) {
            $value['pics'] =  (!empty($value['pics'])) ? explode(',', $value['pics']) : [];
            $value['audios'] = (!empty($value['audios'])) ? explode(',', $value['audios']) : [];
        }
        apiReturn(200,'获取成功',['list'=>$list,'count'=>$count]);
    }

    // 新增操作记录
    public function orderRecordInsert()
    {
        $post = input();//order_id content pic
        if (empty($post['content'])) {
            apiReturn(201,'内容不能为空');
        }
        if (!isset($post['pic'])) {
            $post['pic'] = '';
        }
        $this->orderRecordAdd($post['order_id'], $this->username, $post['content'], $post['pic']);
        apiReturn(200,'提交成功');
    }

    // 订单操作列表
    public function orderRecordList()
    {
        $post = input();
        if (!isset($post['order_id'])) {
            apiReturn(201,'参数不完整');
        }
        $list = db('crm_order_record')->where('order_id',$post['order_id'])->order('date DESC')->select();
        apiReturn(200,'获取成功',$list);
    }

    // 删除
    public function delete()
    {
        $post = input();
        if (!isset($post['id'])) {
            apiReturn(201,'参数不完整');
        }
        if ($this->table->where('id',$post['id'])->delete()) {
            apiReturn(200,'删除成功');
        }else{
            apiReturn(201,'删除失败');
        }
    }

    // 分配后端
    public function orderFenpei()
    {
        $post = input();//order_id houduan_manage_id
        if (!isset($post['order_id']) || !isset($post['houduan_manage_id'])) {
            apiReturn(201,'参数不完整');
        }
        $manageInfo1 = db('crm_manage')->alias('a')->join('tp_crm_fenzu b','a.zu_id=b.id')->field('a.username,a.name,b.id,b.name as zu_name')->where('a.username',$post['houduan_manage_id'])->find();
        if (empty($manageInfo1)) {
            apiReturn(201,'工号不存在');
        }
        $datas['houduan_manage_id'] = $manageInfo1['username'];
        $datas['houduan_manage_name'] = $manageInfo1['name'];
        $datas['houduan_zu_id'] = $manageInfo1['id'];
        $datas['houduan_zu_name'] = $manageInfo1['zu_name'];
        $this->table->where('order_id',$post['order_id'])->update($datas);
        apiReturn(200,'分配成功');
    }

    // 设置订单状态
    public function setOrderStatus()
    {
        $post = input();//order_id order_status
        //order_type 1 律师函(0成交 1经理审核 2出律师函 3发律师函 4法催 6回款 7完结 8取消)
        //order_type 2 立案起诉(0成交 1经理审核 2签合同 3付尾款 4立案起诉 5开庭 6回款 7完结 8取消)
        if (!isset($post['order_id']) || !isset($post['order_status'])) {
            apiReturn(201,'参数错误');
        }
        $order = db('crm_order')->where('order_id',$post['order_id'])->find();
        if (empty($order)) {
            apiReturn(201,'订单不存在');
        }
        $datas = [];
        $content = '';
        if ($order['order_type'] == 1) {
            if ($post['order_status'] == 1) {
                $datas['order_status'] = $post['order_status'];
                $datas['shenhe_date'] = time();
                $content = '订单审核';
            }elseif ($post['order_status'] == 2) {
                $datas['order_status'] = $post['order_status'];
                $content = '签合同';
            }elseif ($post['order_status'] == 4) {
                $datas['order_status'] = $post['order_status'];
                $content = '出律师函';
            }elseif ($post['order_status'] == 5) {
                $datas['order_status'] = $post['order_status'];
                $content = '发律师函';
            }elseif ($post['order_status'] == 6) {
                $datas['order_status'] = $post['order_status'];
                $content = '法催';
            }elseif ($post['order_status'] == 9) {
                $datas['order_status'] = $post['order_status'];
                $content = '回款';
            }elseif ($post['order_status'] == 10) {
                $datas['order_status'] = $post['order_status'];
                $content = '完结';
            }elseif ($post['order_status'] == 11) {
                $datas['order_status'] = $post['order_status'];
                $content = '取消';
            }else{
                apiReturn(201,'订单类型错误');
            }
        }elseif ($order['order_type'] == 2) {
            if ($post['order_status'] == 1) {
                $datas['order_status'] = $post['order_status'];
                $datas['shenhe_date'] = time();
                $content = '订单审核';
            }elseif ($post['order_status'] == 2) {
                $datas['order_status'] = $post['order_status'];
                $content = '签合同';
            }elseif ($post['order_status'] == 3) {
                $datas['order_status'] = $post['order_status'];
                $content = '付尾款';
            }elseif ($post['order_status'] == 4) {
                $datas['order_status'] = $post['order_status'];
                $content = '出律师函';
            }elseif ($post['order_status'] == 5) {
                $datas['order_status'] = $post['order_status'];
                $content = '发律师函';
            }elseif ($post['order_status'] == 6) {
                $datas['order_status'] = $post['order_status'];
                $content = '法催';
            }elseif ($post['order_status'] == 7) {
                $datas['order_status'] = $post['order_status'];
                $content = '立案起诉';
            }elseif ($post['order_status'] == 8) {
                $datas['order_status'] = $post['order_status'];
                $content = '开庭';
            }elseif ($post['order_status'] == 9) {
                $datas['order_status'] = $post['order_status'];
                $content = '回款';
            }elseif ($post['order_status'] == 10) {
                $datas['order_status'] = $post['order_status'];
                $content = '完结';
            }elseif ($post['order_status'] == 11) {
                $datas['order_status'] = $post['order_status'];
                $content = '取消';
            }else{
                apiReturn(201,'订单类型错误');
            }
        }else{
            apiReturn(201,'订单类型错误');
        }

        $this->table->save($datas,['order_id'=>$post['order_id']]);
        $this->orderRecordAdd($post['order_id'], $this->username, $content);

        apiReturn(200,'操作成功');
    }

}
